#include "mypushbutton.h"
#include <QDebug>
#include <QPropertyAnimation>

//MyPushButton::MyPushButton()
//{

//}

MyPushButton::MyPushButton(QString normalImg, QString pressImg)
{
    this->normalImgPath = normalImg;
    if (pressImg != "")
    {
        this->pressImgPath = pressImg;
    }

    QPixmap pix;
    //加载失败是false
    bool ret = pix.load(normalImg);
    if (!ret)
    {
        qDebug() << "图片加载失败" ;
        return;
    }

    //设置按钮大小
    this->setFixedSize(QSize(pix.width(), pix.height()));

    //设置不规则图片样式
    //设置PNG格式没有边框
    this->setStyleSheet("QPushButton{border:0px;}");

    //设置图标
    this->setIcon(pix);

    //设置图标大小
    this->setIconSize(QSize(pix.width(), pix.height()));

}

//向下跳
void MyPushButton::zoom1()
{
    //创建动画特效
    QPropertyAnimation * animation = new QPropertyAnimation(this, "geometry");
    //设置动画时间间隔
    animation->setDuration(300);
    //设置起始位置
    animation->setStartValue(QRect(this->x(), this->y(), this->width(), this->height()));
    //设置结束位置
    animation->setEndValue(QRect(this->x(), this->y()+30, this->width(), this->height()));
    //设置弹跳曲线
    animation->setEasingCurve(QEasingCurve::OutElastic);
    //执行动画
    animation->start();
}

//向上跳
void MyPushButton::zoom2()
{
    //创建动画特效
    QPropertyAnimation * animation = new QPropertyAnimation(this, "geometry");
    //设置动画时间间隔
    animation->setDuration(1000);
    //设置起始位置
    animation->setStartValue(QRect(this->x(), this->y()+30, this->width(), this->height()));
    //设置结束位置
    animation->setEndValue(QRect(this->x(), this->y(), this->width(), this->height()));
    //设置弹跳曲线
    animation->setEasingCurve(QEasingCurve::OutElastic);
    //执行动画
    animation->start();
}

void MyPushButton::mousePressEvent(QMouseEvent *e)
{
    if(this->pressImgPath != "")//传入按下图片不为空
    {
//        qDebug() << "hehehe";
        QPixmap pix;
        bool ret = pix.load(pressImgPath);
        if (!ret)
        {
            qDebug() << "图片加载失败" ;
            return;
        }

        //设置按钮大小
        this->setFixedSize(QSize(pix.width(), pix.height()));

        //设置不规则图片样式
        //设置PNG格式没有边框
        this->setStyleSheet("QPushButton{border:0px;}");

        //设置图标
        this->setIcon(pix);

        //设置图标大小
        this->setIconSize(QSize(pix.width(), pix.height()));
    }
    //让父类执行其他操作
    //void类型返回值，依然可以用return
    return QPushButton::mousePressEvent(e);

}

void MyPushButton::mouseReleaseEvent(QMouseEvent *e)
{

    if(this->normalImgPath != "")//传入按下图片不为空
    {
//        qDebug() << "哈哈哈哈";
        QPixmap pix;
        bool ret = pix.load(normalImgPath);
        if (!ret)
        {
            qDebug() << "图片加载失败" ;
            return;
        }

        //设置按钮大小
        this->setFixedSize(QSize(pix.width(), pix.height()));

        //设置不规则图片样式
        //设置PNG格式没有边框
        this->setStyleSheet("QPushButton{border:0px;}");

        //设置图标
        this->setIcon(pix);

        //设置图标大小
        this->setIconSize(QSize(pix.width(), pix.height()));
    }
    //让父类执行其他操作
    //void类型返回值，依然可以用return
    return QPushButton::mouseReleaseEvent(e);
}
